package cn.itcast.oa.action;


import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import cn.itcast.oa.base.BaseAction;
import cn.itcast.oa.model.Application;
import cn.itcast.oa.model.ApplicationTemplate;
import cn.itcast.oa.model.ApproveInfo;
import cn.itcast.oa.model.TaskView;
import cn.itcast.oa.util.HqlHelper;

@Controller
@Scope("prototype")                                                                                                             
public class FlowAction extends BaseAction {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private Long applicationTemplateId;
	private File upLoad;
	
	private Long applicationId;
	private String taskId;
	private String comment;
	private boolean approval;
	
	private String outcome;
	private String applicationStatus;

	
	public String getApplicationStatus() {
		return applicationStatus;
	}

	public void setApplicationStatus(String applicationStatus) {
		this.applicationStatus = applicationStatus;
	}

	public String applicationTemplateList() throws Exception{
		List<ApplicationTemplate> applicationTemplateList = applicationTemplateService.findAll();
		ActionContext.getContext().put("applicationTemplateList", applicationTemplateList);
		return "applicationTemplateList";
	}
	
	public String submitUI() throws Exception{
		//System.out.println("---------------"+applicationTemplateId);
		return "submitUI";
	}
	
	public String submit() throws Exception{
		Application application=new Application();
		
		application.setApplicant(getCurrentUser());
		application.setPath(saveUploadFile(upLoad));
		application.setApplicationTemplate(applicationTemplateService.getById(applicationTemplateId));
		
		applicationService.submit(application);
		return "toMyApplicationList";
	}
	
	public String myTaskList() throws Exception{
		List<TaskView> taskViewList=applicationService.getMyTaskViewList(getCurrentUser());
		ActionContext.getContext().put("taskViewList", taskViewList);
		long count=0;
		if(taskViewList!=null){
			count=taskViewList.size();
		}
		ActionContext.getContext().put("count", count);
		return "myTaskList";
	}
	
	/**
	 * 审批页面
	 * @return
	 * @throws Exception
	 */
	public String approveUI() throws Exception{	
		Set<String> outcomes=applicationService.getOutComesByTaskId(taskId);
		ActionContext.getContext().put("outcomes", outcomes);
		return "approveUI";
	}
	
	public String approve() throws Exception{	
		ApproveInfo approveInfo=new ApproveInfo();
		
		approveInfo.setComment(comment);
		approveInfo.setApproval(approval);
		approveInfo.setApprover(getCurrentUser());
		approveInfo.setApplication(applicationService.getById(applicationId));
		approveInfo.setApproveTime(new Date());
		
		applicationService.approve(approveInfo,taskId,outcome);
		return "toMyTaskList";
	}
	public String approveHistory() throws Exception{
		Application application=applicationService.getById(applicationId);
		ActionContext.getContext().put("approveInfos", application.getApproveInfos());
		return "approveHistory";
	}
	public String myApplicationList() throws Exception{
		// 构建查询条件并准备分页信息
			new HqlHelper(Application.class, "a")//
						.addCondition("a.applicant=?", getCurrentUser())//
						.addCondition(applicationTemplateId != null, "a.applicationTemplate.id=?", applicationTemplateId)//
						.addCondition(StringUtils.isNotBlank(applicationStatus), "a.status=?", applicationStatus)//
						.addOrder("a.applyTime", false)//
						.buildPageBeanForStruts2(pageNum, applicationService);
		  
				// 准备数据
				List<ApplicationTemplate> applicationTemplateList = applicationTemplateService.findAll();
				ActionContext.getContext().put("applicationTemplateList", applicationTemplateList);

				return "myApplicationList";
	}
    
	//----------
	public Long getApplicationTemplateId() {
		return applicationTemplateId;
	}

	public void setApplicationTemplateId(Long applicationTemplateId) {
		this.applicationTemplateId = applicationTemplateId;
	}

	public File getUpLoad() {
		return upLoad;
	}

	public void setUpLoad(File upLoad) {
		this.upLoad = upLoad;
	}

	public String getTaskId() {
		return taskId;
	}

	public void setTaskId(String taskId) {
		this.taskId = taskId;
	}
	

	public Long getApplicationId() {
		return applicationId;
	}

	public void setApplicationId(Long applicationId) {
		this.applicationId = applicationId;
	}


	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}
	

	public boolean isApproval() {
		return approval;
	}

	public void setApproval(boolean approval) {
		this.approval = approval;
	}

	public String getOutcome() {
		return outcome;
	}

	public void setOutcome(String outcome) {
		this.outcome = outcome;
	}

}
